package com.qrxl.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qrxl.common.exception.CustomException;
import com.qrxl.enums.EnumSysBoolean;
import com.qrxl.enums.EnumSysFlag;
import com.qrxl.enums.EnumUserType;
import com.qrxl.user.entity.UserLogin;
import com.qrxl.user.mapper.UserLoginMapper;
import com.qrxl.user.service.IRoleService;
import com.qrxl.user.service.IUserLoginService;
import com.qrxl.user.vo.UserLoginInfo;
import com.qrxl.user.vo.req.UserAddReq;
import com.qrxl.user.vo.req.UserUpdateReq;
import com.qrxl.util.JGAssert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author zhoukun
 * @since 2020-12-01
 */
@Service
public class UserLoginServiceImpl extends ServiceImpl<UserLoginMapper, UserLogin> implements IUserLoginService {

    @Autowired
    IRoleService roleService;
    @Override
    public UserLogin selectOneByLoginName(String loginName) {
        JGAssert.hasText(loginName, "loginName不能为空");
        UserLogin cond = new UserLogin();
        cond.setLoginName(loginName);
        return getBaseMapper().selectOne(Wrappers.query(cond));
    }

    @Override
    public void updateVo(UserUpdateReq req, UserLoginInfo ul) {
        UserLogin updateObj = new UserLogin();
        BeanUtils.copyProperties(req, updateObj);
        updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserLogin add(UserAddReq req, UserLoginInfo ul) {
        UserLogin one = new UserLogin();
        BeanUtils.copyProperties(req, one);
        if (EnumUserType.USER.getCode().equals(ul.getType())) {
            throw new CustomException("普通用户不能新建账号");
        }
        if (req.getValidFlag() == null) {
            one.setValidFlag(EnumSysFlag.YES.getCode());
        }
        //判断用户名是否重复
        JGAssert.isNull(selectOneByLoginName(req.getLoginName()), "用户名已经存在");
        baseMapper.insert(one);
        //todo 如果添加组织机构默认关联部门管理员角色
        return one;
    }



    @Override
    public UserLoginInfo login(String loginName, String password) {
        String errorMsg = "用户名或密码错误";
        UserLogin userLogin = selectOneByLoginName(loginName);
        JGAssert.notNull(userLogin, errorMsg);
        JGAssert.isTrue(userLogin.getPassword().equalsIgnoreCase(password), errorMsg);
        JGAssert.isTrue(EnumSysFlag.YES.getCode().equals(userLogin.getValidFlag()), "用户被停用");
        UserLoginInfo ul = new UserLoginInfo();
        ul.setLoginName(userLogin.getLoginName());
        ul.setId(userLogin.getId());
        ul.setUserName(userLogin.getUserName());
        ul.setType(userLogin.getType());
        ul.setRoleList(roleService.selectListByUserId(userLogin.getId()));
        return ul;
    }

    @Override
    public EnumSysBoolean userExist(String loginName) {
        if (selectOneByLoginName(loginName) != null) {
            return EnumSysBoolean.YES;
        }
        return EnumSysBoolean.NO;
    }
}